import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
import pyautogui as pg
import pywinauto
from pywinauto.keyboard import send_keys
from selenium.common import exceptions
import xlwings as xw
import os


class whisky_off_web:

    def __init__(self, whisky_link, Scotland_link, whiskyshop_link, whisky_jd_link):
        self.whisky_link = whisky_link
        self.Scotland_link = Scotland_link
        self.whiskyshop_link = whiskyshop_link
        self.whisky_jd_link = whisky_jd_link

    # 这个是官网的数据拉取
    def in_whisky_website(self):
        # 打开浏览器的一些配置
        wb = webdriver.Chrome()
        wb.implicitly_wait(30)
        wb.get(self.whisky_link)
        wb.maximize_window()
        # 睡眠两秒，让浏览器完全启动
        time.sleep(2)
        # 用于获取有多少页
        page_num_all = wb.find_elements(By.CLASS_NAME, 'page-numbers')[8]
        page_num_all_text = page_num_all.text
        # 将价格和名字录入进Excel
        # 初始化
        app = xw.App(visible=False, add_book=False)
        wd = app.books.open(r"D:/whisky_website.xlsx")
        # 进入第一个报表页
        sheet = wd.sheets["sheet1"]
        wd.sheets["sheet1"].range('A1').value = '商品名'
        wd.sheets["sheet1"].range('B1').value = '价格'
        wd.sheets["sheet1"].range('C1').value = '商品图片'
        # 做一个空数组
        wine_name_li = []
        wine_price_li = []
        wine_photo_li = []
        # 这个数字，用于给图片起名字
        path_name_num = 0
        for website_page_num in range(int(page_num_all_text)):
            website_page_num += 1
            # 获取页面上有多少个商品
            website_shop_size = wb.find_elements(By.CLASS_NAME, "ast-col-sm-12")
            for website_range in range(len(website_shop_size)):
                # 获取文字和图片
                wine_name = wb.find_elements(By.CLASS_NAME, "woocommerce-loop-product__title")[website_range]
                wine_ses = wine_name.text
                wine_name_li.append(wine_ses)
                wine_price = wb.find_elements(By.CSS_SELECTOR, ".astra-shop-summary-wrap .price")[website_range]
                wine_pri = wine_price.text
                wine_price_li.append(wine_pri)
                # 进入详情页，用于获取大图
                wb.find_elements(By.CSS_SELECTOR, ".astra-shop-thumbnail-wrap .woocommerce-LoopProduct-link img")[
                    website_range].click()
                time.sleep(2)
                img_dj = wb.find_element(By.CLASS_NAME, "zoomImg")
                # 点击鼠标右键
                ActionChains(wb).context_click(img_dj).perform()
                time.sleep(1)
                # 按下下键
                pg.press("down")
                pg.press("down")
                pg.press("enter")
                time.sleep(1)
                # 用于操作弹出的“另存为”弹窗，这用于初始化
                Pwd = pywinauto.Desktop()
                # 锁定文件保存窗口
                File_Window = Pwd["另存为"]
                time.sleep(1)
                # 下方是文件地址框输入
                File_Window["Toolbar3"].click()
                time.sleep(1)
                path_name_num += 1
                # 输入路径
                send_keys(r"D:\whisky_off_website\{0}".format(str(path_name_num) + ".jpg"))
                time.sleep(2)
                # 输入回车键
                send_keys("{VK_RETURN}")
                time.sleep(2)
                wb.find_element(By.CLASS_NAME, "back-to-shop").click()
                time.sleep(2)
            # 用于判断，如果等于最大页面，就停止翻页
            time.sleep(2)
            if website_page_num < int(page_num_all_text):
                # 第一页爬完后，就翻页
                wb.find_element(By.CLASS_NAME, "next").click()

        # 获取文件夹下有多少文件，用于待会的循环
        file_num = os.listdir(r"D:\whisky_off_website")
        for file_index in file_num:
            wine_photo_li.append(file_index)
        for wine_name_li_index in range(len(wine_name_li)):
            wd.sheets["sheet1"].range('A{0}'.format(wine_name_li_index + 2)).value = '{0}'.format(wine_name_li[wine_name_li_index])
        for wine_price_li_index in range(len(wine_price_li)):
            wd.sheets["sheet1"].range('B{0}'.format(wine_price_li_index + 2)).value = '{0}'.format(wine_price_li[wine_price_li_index])
        # 将导出的图片导入Excel中，居中导入
        for wine_photo_index in range(len(wine_photo_li)):
            wine_photo_index += 1
            file_name = os.path.join(os.getcwd(), r"D:\whisky_off_website\{0}.jpg".format(wine_photo_index))
            rng = sheet.range('C{0}'.format(wine_photo_index))
            width, height = 80, 80
            # 把图片居中在Excel格子中
            left = rng.left + (rng.width - width) / 2
            top = rng.top + (rng.height - height) / 2
            sheet.pictures.add(file_name, left=left, top=top, width=width, height=height)

        time.sleep(1)
        wd.save(r"D:/whisky_website.xlsx")
        wd.close()
        app.quit()
        wb.close()
        wb.quit()
        # os.remove()  # 到时候再说
        self.in_Scotland_link()

    # 这个是官网另外一个页面的数据拉取程序
    def in_Scotland_link(self):
        # 打开浏览器的一些配置
        wb = webdriver.Chrome()
        wb.implicitly_wait(5)
        wb.get(self.Scotland_link)
        wb.maximize_window()
        # 睡眠两秒，让浏览器完全启动
        time.sleep(2)
        # 获取总页数
        page_num_all = wb.find_element(By.CLASS_NAME, "js-paging__total-value")
        # 做一个数据存储
        page_num_all_text = page_num_all.text
        # 获取商品的名字信息，一个页面有24条数据，但是还是要获取
        # 先把广告弹窗关了
        adv = wb.find_elements(By.CLASS_NAME, "termly-styles-module-root-115422")[1]
        adv.click()
        time.sleep(2)
        # 将价格和名字录入进Excel
        # 初始化
        app = xw.App(visible=False, add_book=False)
        wd = app.books.open(r"D:/scotland_whisky.xlsx")
        # 进入第一个报表页
        sheet = wd.sheets[0]
        wd.sheets["sheet1"].range('A1').value = '商品名'
        wd.sheets["sheet1"].range('B1').value = '价格'
        wd.sheets["sheet1"].range('C1').value = '图片'
        # 创建两个数组，用于保存酒名和价格
        whisky_shop_name_li = []
        whisky_shop_price_li = []
        whisky_shop_photo_li = []
        photo_name = 0
        # 分页
        for page_num_index in range(int(page_num_all_text)):
            page_num_index += 1
            shop_size = wb.find_elements(By.CLASS_NAME, "product-card__content")
            # 用于获取页面中的数据
            for shop_size_index in range(len(shop_size)):
                # 获取商品名字
                whisky_shop_name = wb.find_elements(By.CLASS_NAME, "product-card__content")[shop_size_index]
                # 值的转换，用于去除/符号
                whisky_shop_name_te = whisky_shop_name.text
                whisky_shop_name_li.append(whisky_shop_name_te)
                # 获取商品价格
                whisky_shop_price = wb.find_elements(By.CLASS_NAME, "product-card__price")[shop_size_index]
                whisky_price_session = whisky_shop_price.text
                whisky_shop_price_li.append(whisky_price_session)
                time.sleep(0.2)
                # 点击进入商品详情，准备拿图片
                try:
                    ActionChains(wb).click(whisky_shop_name).perform()
                    time.sleep(2)
                except exceptions.TimeoutException:
                    time.sleep(60)
                try:
                    wine_photo = wb.find_element(By.CLASS_NAME, "product-main__image")
                    # 这个网站有些商品页不一样，暂时用这个方式处理
                    # 点击鼠标右键
                    ActionChains(wb).context_click(wine_photo).perform()
                    time.sleep(1)
                    # 按下下键
                    pg.press("down")
                    pg.press("down")
                    pg.press("enter")
                    time.sleep(2)
                    # 用于操作弹出的“另存为”弹窗，这用于初始化
                    Pwd = pywinauto.Desktop()
                    # 锁定文件保存窗口
                    File_Window = Pwd["另存为"]
                    time.sleep(2)
                    # 下方是文件地址框输入
                    File_Window["Toolbar3"].click()
                    time.sleep(1)
                    # 使用数字来命名
                    photo_name += 1
                    # 输入路径
                    send_keys(r"D:\whisky_Scotland\{0}".format(str(photo_name) + ".jpg"))
                    time.sleep(1)
                    # 输入回车键
                    send_keys("{VK_RETURN}")
                    time.sleep(2)
                    wb.back()
                except exceptions.NoSuchElementException:
                    wine_photo_new = wb.find_element(By.CLASS_NAME, "whiskylabels-intro__bottle")
                    # 点击鼠标右键
                    ActionChains(wb).context_click(wine_photo_new).perform()
                    time.sleep(1)
                    # 按下下键
                    pg.press("down")
                    pg.press("down")
                    pg.press("enter")
                    time.sleep(1)
                    # 用于操作弹出的“另存为”弹窗，这用于初始化
                    Pwd = pywinauto.Desktop()
                    # 锁定文件保存窗口
                    File_Window = Pwd["另存为"]
                    time.sleep(1)
                    # 下方是文件地址框输入
                    File_Window["Toolbar3"].click()
                    time.sleep(1)
                    # 数字名
                    photo_name += 1
                    # 输入路径
                    send_keys(r"D:\whisky_Scotland\{0}".format(str(photo_name) + ".jpg"))
                    time.sleep(1)
                    # 输入回车键
                    send_keys("{VK_RETURN}")
                    time.sleep(2)
                    wb.back()
            if page_num_index < int(page_num_all_text):
                wb.find_elements(By.CLASS_NAME, "paging__button")[1].click()
            time.sleep(2)
        # 获取文件夹下有多少文件，用于待会的循环
        file_num = os.listdir(r"D:\whisky_Scotland")
        for file_index in file_num:
            whisky_shop_photo_li.append(file_index)
        for whisky_shop_name_li_index in range(len(whisky_shop_name_li)):
            wd.sheets["sheet1"].range('A{0}'.format(whisky_shop_name_li_index + 2)).value = '{0}'.format(whisky_shop_name_li[whisky_shop_name_li_index])
        for whisky_shop_price_li_index in range(len(whisky_shop_price_li)):
            wd.sheets["sheet1"].range('B{0}'.format(whisky_shop_price_li_index + 2)).value = '{0}'.format(whisky_shop_price_li[whisky_shop_price_li_index])
        # 将导出的图片导入Excel中，居中导入
        for wine_photo_index in range(len(whisky_shop_photo_li)):
            wine_photo_index += 1
            file_name = os.path.join(os.getcwd(), r"D:\whisky_Scotland\{0}.jpg".format(wine_photo_index))
            rng = sheet.range('C{0}'.format(wine_photo_index))
            width, height = 80, 80
            # 把图片居中在Excel格子中
            left = rng.left + (rng.width - width) / 2
            top = rng.top + (rng.height - height) / 2
            sheet.pictures.add(file_name, left=left, top=top, width=width, height=height)

        wd.save(r"D:/scotland_whisky.xlsx")
        wd.close()
        app.quit()
        wb.close()
        wb.quit()
        time.sleep(2)
        self.in_whiskyshop_link()

    # 威士忌商店
    def in_whiskyshop_link(self):
        # 打开浏览器的一些配置
        wb = webdriver.Chrome()
        wb.implicitly_wait(10)
        wb.get(self.whiskyshop_link)
        wb.maximize_window()
        # 等待浏览器完全启动
        time.sleep(3)
        # 这里是用于关闭登录弹窗的，时有时无
        try:
            wb.find_elements(By.CLASS_NAME, "PopupCloseButton__InnerPopupCloseButton-sc-957qyh-0")[0].click()
        except exceptions.NoSuchElementException:
            wb.find_elements(By.CLASS_NAME, "PopupCloseButton__InnerPopupCloseButton-sc-957qyh-0")[1].click()
        except exceptions.ElementNotInteractableException:
            wb.find_elements(By.CLASS_NAME, "PopupCloseButton__InnerPopupCloseButton-sc-957qyh-0")[1].click()
        # 这个是用来关闭下方cookie框框的~
        try:
            wb.find_element(By.ID, "btn-cookie-allow").click()
        except exceptions.NoSuchElementException:
            print("消失了")
            pass
        # 获取总页数，这里总数比较麻烦，需要做计算才能获取大概有多少页
        shop_all_ss = wb.find_elements(By.CLASS_NAME, "toolbar-number")[2]
        # 获取到总页数
        page_calculate = int(shop_all_ss.text) / 100
        page_calculate_session = page_calculate.__round__()

        # 将价格和名字录入进Excel
        # 初始化
        app = xw.App(visible=False, add_book=False)
        wd = app.books.open(r"D:/whisky_shop.xlsx")
        # 进入第一个报表页
        sheet = wd.sheets[0]
        wd.sheets["sheet1"].range('A1').value = '商品名'
        wd.sheets["sheet1"].range('B1').value = '价格'
        wd.sheets["sheet1"].range('C1').value = '图片'
        # 用数组存储商品名和价格
        shop_name_li = []
        shop_price_li = []
        shop_photo_li = []
        photo_name = 0

        # 用总数做个翻页判断
        for shop_store_all in range(page_calculate_session):
            shop_store_all += 1
            # 获取一页中总商品数量
            shop_all_index = wb.find_elements(By.CLASS_NAME, "product-item-info")
            # 获取单个页面的所有商品数据
            for shop_index in range(len(shop_all_index)):
                # 商品名
                shop_name = wb.find_elements(By.CLASS_NAME, "product-item-link")[shop_index]
                shop_name_session = shop_name.text
                shop_name_li.append(shop_name_session)
                # 价格这里比较麻烦
                try:
                    # 商品价格
                    shop_in_store = wb.find_elements(By.CLASS_NAME, "price-box")[shop_index]
                    shoop_store_session = shop_in_store.text
                    shop_price_li.append(shoop_store_session)
                except exceptions.NoSuchElementException:
                    pass
                time.sleep(0.1)
                # 获取页面中图片，图片点击事件
                wb.find_elements(By.CLASS_NAME, "product-image-photo")[shop_index].click()
                # 睡眠两秒，等待页面加载完
                time.sleep(2)
                # 获取详情页大图信息
                big_photo = wb.find_element(By.CLASS_NAME, "no-sirv-lazy-load")
                # 点击鼠标右键
                ActionChains(wb).context_click(big_photo).perform()
                time.sleep(1)
                # 按下下键
                for down_index in range(8):
                    pg.press("down")
                pg.press("enter")
                time.sleep(1)
                # 用于操作弹出的“另存为”弹窗，这用于初始化
                Pwd = pywinauto.Desktop()
                # 锁定文件保存窗口
                File_Window = Pwd["另存为"]
                time.sleep(1)
                # 下方是文件地址框输入
                File_Window["Toolbar3"].click()
                time.sleep(1)
                # 数字起名
                photo_name += 1
                # 输入路径
                send_keys(r"D:\whisky_shop\{0}".format(str(photo_name) + ".jpg"))
                time.sleep(1)
                # 输入回车键
                send_keys("{VK_RETURN}")
                time.sleep(1.1)
                wb.back()
            time.sleep(2)
            # 做翻页操作
            if shop_store_all < page_calculate_session:
                fanye = wb.find_elements(By.CLASS_NAME, "pages-item-next")[1]
                ActionChains(wb).click(fanye).perform()
        # 获取文件夹下有多少文件，用于待会的循环
        file_num = os.listdir(r"D:\whisky_shop")
        for file_index in file_num:
            shop_photo_li.append(file_index)
        # 拼接数组，然后将数组中商品名和商品价格写入到Excel
        for shop_name_li_index in range(len(shop_name_li)):
            wd.sheets["sheet1"].range('A{0}'.format(shop_name_li_index + 2)).value = '{0}'.format(shop_name_li[shop_name_li_index])
        for shop_pri_li_index in range(len(shop_price_li)):
            wd.sheets["sheet1"].range('B{0}'.format(shop_pri_li_index + 2)).value = '{0}'.format(shop_price_li[shop_pri_li_index])
        # 将导出的图片导入Excel中，居中导入
        for wine_photo_index in range(len(shop_photo_li)):
            wine_photo_index += 1
            file_name = os.path.join(os.getcwd(), r"D:\whisky_shop\{0}.jpg".format(wine_photo_index))
            rng = sheet.range('C{0}'.format(wine_photo_index))
            width, height = 80, 80
            # 把图片居中在Excel格子中
            left = rng.left + (rng.width - width) / 2
            top = rng.top + (rng.height - height) / 2
            sheet.pictures.add(file_name, left=left, top=top, width=width, height=height)
        wd.save(r"D:/whisky_shop.xlsx")
        wd.close()
        app.quit()
        wb.close()
        wb.quit()
        time.sleep(1)
        self.in_jd_link()

    # 爬取京东的数据
    def in_jd_link(self):
        # 打开浏览器的一些配置
        wb = webdriver.Chrome()
        wb.implicitly_wait(10)
        wb.get(self.whisky_jd_link)
        wb.maximize_window()
        # 等待浏览器完全启动
        # 往下翻滚页面，不然加载不出所有的商品信息
        coord_js = "window.scrollTo(0,document.body.scrollHeight)"
        wb.execute_script(coord_js)
        time.sleep(1.5)
        # 获取总共有多少页面
        all_page = wb.find_element(By.CSS_SELECTOR, ".p-skip b")
        all_page_session = all_page.text
        # 将价格和名字录入进Excel
        # 初始化
        app = xw.App(visible=False, add_book=False)
        wd = app.books.open(r"D:/jd_whisky.xlsx")
        # 进入第一个报表页
        sheet = wd.sheets[0]
        wd.sheets["sheet1"].range('A1').value = '商品名'
        wd.sheets["sheet1"].range('B1').value = '价格'
        wd.sheets["sheet1"].range('C1').value = '店铺名'
        wd.sheets["sheet1"].range('D1').value = '图片'
        # 使用数组，用于保存名称、价格和店名
        jd_name_li = []
        jd_price_li = []
        jd_store_li = []
        jd_photo_li = []
        photo_name = 0

        # 循环拉取单个页面数据多少次
        for all_page_index in range(int(all_page_session)):
            # 往下翻滚页面，不然加载不出所有的商品信息
            coord_js = "window.scrollTo(0,document.body.scrollHeight)"
            wb.execute_script(coord_js)
            time.sleep(1.5)
            all_page_index += 1
            # 获取所有商品的div，用于区分一页有多少商品需要爬取
            shop_div = wb.find_elements(By.CLASS_NAME, "gl-i-wrap")
            # 循环，这个是一个页面的商品拉取循环
            for one_page_shop_index in range(len(shop_div)):
                # 获取整个页面的商品价格
                jd_whisky_price = wb.find_elements(By.CSS_SELECTOR, ".gl-i-wrap .p-price")[one_page_shop_index]
                whisky_price_session = jd_whisky_price.text
                jd_price_li.append(whisky_price_session)
                # 获取整个页面的商品名称
                jd_whisky_name = wb.find_elements(By.CSS_SELECTOR, ".gl-i-wrap .p-name a em")[one_page_shop_index]
                whisky_name_session = jd_whisky_name.text
                jd_name_li.append(whisky_name_session)
                # 获取整个页面对应商品的售卖店铺
                jd_whisky_store_name = wb.find_elements(By.CSS_SELECTOR, ".gl-i-wrap .p-shop")[one_page_shop_index]
                whisky_store_session = jd_whisky_store_name.text
                jd_store_li.append(whisky_store_session)
                # 获取主页的图片
                jd_photo = wb.find_elements(By.CSS_SELECTOR, ".gl-i-wrap .p-img")[one_page_shop_index]
                # 点击鼠标右键
                ActionChains(wb).context_click(jd_photo).perform()
                time.sleep(1)
                # 按下下键
                for down_index in range(8):
                    pg.press("down")
                pg.press("enter")
                time.sleep(1)
                # 用于操作弹出的“另存为”弹窗，这用于初始化
                Pwd = pywinauto.Desktop()
                # 锁定文件保存窗口
                File_Window = Pwd["另存为"]
                time.sleep(1)
                # 下方是文件地址框输入
                File_Window["Toolbar3"].click()
                time.sleep(1)
                # 数字起名
                photo_name += 1
                # 输入路径
                send_keys(r"D:\whisky_jd\{0}".format(str(photo_name) + ".jpg"))
                time.sleep(1)
                # 输入回车键
                send_keys("{VK_RETURN}")
                time.sleep(1)
            # 爬完了一个页面，就点击下一页
            if all_page_index < int(all_page_session):
                wb.find_element(By.CLASS_NAME, "pn-next").click()

        # 获取文件夹下有多少文件，用于待会的循环
        file_num = os.listdir(r"D:\whisky_jd")
        for file_index in file_num:
            jd_photo_li.append(file_index)
        for jd_name_li_index in range(len(jd_name_li)):
            wd.sheets["sheet1"].range('A{0}'.format(jd_name_li_index + 2)).value = '{0}'.format(jd_name_li[jd_name_li_index])
        for jd_price_li_index in range(len(jd_price_li)):
            wd.sheets["sheet1"].range('B{0}'.format(jd_price_li_index + 2)).value = '{0}'.format(jd_price_li[jd_price_li_index])
        for jd_store_li_index in range(len(jd_store_li)):
            wd.sheets["sheet1"].range('C{0}'.format(jd_store_li_index + 2)).value = '{0}'.format(jd_store_li[jd_store_li_index])
        # 将导出的图片导入Excel中，居中导入
        for wine_photo_index in range(len(jd_photo_li)):
            wine_photo_index += 1
            file_name = os.path.join(os.getcwd(), r"D:\whisky_jd\{0}.jpg".format(wine_photo_index))
            rng = sheet.range('C{0}'.format(wine_photo_index))
            width, height = 80, 80
            # 把图片居中在Excel格子中
            left = rng.left + (rng.width - width) / 2
            top = rng.top + (rng.height - height) / 2
            sheet.pictures.add(file_name, left=left, top=top, width=width, height=height)
        wd.save(r"D:/jd_whisky.xlsx")
        wd.close()
        app.quit()
        wb.close()
        wb.quit()
        time.sleep(2)
        print("完成")


whisky_off_web(whisky_link=r"https://www.simplywhisky.sg/shop/",
               Scotland_link=r"https://www.thewhiskyexchange.com/c/40/single-malt-scotch-whisky",
               whiskyshop_link="https://www.whiskyshop.com/single-malt-scotch-whisky",
               whisky_jd_link="https://search.jd.com/search?keyword=%E5%A8%81%E5%A3%AB%E5%BF%8C&qrst=1&wq=%E5%A8%81%E5%A3%AB%E5%BF%8C&stock=1&cid3=14739").in_Scotland_link()  # in_whisky_website 和 in_Scotland_link ，是两个英国网站的方法名，直接用就好
